package pers.zw.study.servlet;

import com.sun.deploy.net.HttpResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Use OutputStream to output Chinese
 */
@WebServlet(name = "ResponseDemo1", urlPatterns = "/ResponseDemo1")
public class ServletResponseDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    private void outputChineseByOutputStream(HttpServletResponse response) throws IOException {
        String data = "中国";
        OutputStream outputStream = response.getOutputStream();
        response.setHeader("content-type", "text/html; charset = UTF-8");

        byte[] dataByteArr = data.getBytes("UTF-8");
        outputStream.write(dataByteArr);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        outputChineseByOutputStream(response);
    }
}
